const app = getApp()
import prizeApi from '../../api/prizeApi'
import addressApi from '../../api/addressApi'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        prizeId: '',
        prize: '',
        address: '',
        content: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.prizeId = options.prizeId
        this.getPrize(options.prizeId)
        this.getAddress()
    },

    async getPrize(prizeId){
        wx.showLoading({
          mask:true
        })
        let data = await  prizeApi.getPrize(prizeId)
    
        wx.hideLoading( )
            
        this.setData({
          prize : data
        })
      },

      chooseAddress(){
          
        wx.chooseAddress({
            success: (res) => {
              console.log(res.userName)
              console.log(res.postalCode)
              console.log(res.provinceName)
              console.log(res.cityName)
              console.log(res.countyName)
              console.log(res.detailInfo)
              console.log(res.nationalCode)
              console.log(res.telNumber)

              this.setData({
                address: res
              })
            }
        })
      },

      inputContent(e){
      
        this.setData({
            content: e.detail.value
        })
      },

      async getAddress(){
        let list = await addressApi.getAddress({
            prizeId: this.data.prizeId
        })
        
        if(list.length > 0){
            this.setData({
                address: list[0],
                content: list[0].content
            })
        }
      },

      async submit(){
        
        if(!this.data.address && !this.data.content){
            wx.showToast({
              title: '没有可提交的内容',
              icon: 'none'
            })
            return
        }
        
        // 发货通知
      wx.requestSubscribeMessage({
        tmplIds: ['_2971CcE5y8IEsaDUPlV1yAsLtgJbYwJ7gx6IKpTWfI'],
        success (res) { }
      })


        wx.showLoading({
            mask: true
        })
        let list = await addressApi.getAddress({
            prizeId: this.data.prizeId
        })
        if(list.length == 0){
            await addressApi.addAdress({
                prizeId: this.data.prizeId,
                content: this.data.content,
                ...this.data.address
            })
        } else {
            delete this.data.address._openid
            delete this.data.address.content
            await addressApi.updateAddres({
                _id: list[0]._id,
                content: this.data.content,
                ...this.data.address
            })
        }
        wx.hideLoading()
      

        wx.showModal({
            title: '提示',
            content: '提交成功',
         
          })

       

        await wx.cloud.callFunction({
          name: 'sendMsg',
          data: {
              type: 'getPrizeSend',
              prizeId: this.data.prizeId,
          }
      })
      
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})